package JavaDesign.report_10.t_2;

/**
 * @author zouran
 * createDate：2023/11/28  22:09
 */
class Geometry implements Cloneable {
    int height;
    Rectangle rectangle;

    Geometry(Rectangle rectangle, int height) {
        this.rectangle = rectangle;
        this.height = height;
    }

    public Object clone() throws CloneNotSupportedException {
        Geometry object = (Geometry) super.clone();
        object.rectangle = (Rectangle) rectangle.clone();
        return object;
    }
}//重写 clone方法://对rectangle进行复制

class Rectangle implements Cloneable {
    double m, n;

    //实现 cloneable接口:
    Rectangle(double m, double n) {
        this.m = m;
        this.n = n;
    }

    public Object clone() throws CloneNotSupportedException //重写clone方法
    {
        Object object = super.clone();
        return object;
    }
}

class ExampleTwo {
    public static void main(String args[]) {
        Geometry geometry = new Geometry(new Rectangle(10, 20), 100);
        try {
            Geometry geometryCopy = (Geometry) geometry.clone();//复制自己
            System.out.println("geometry 对象中的 rectangle矩形的长和宽:");
            System.out.println(geometry.rectangle.m + "," + geometry.rectangle.n);
            System.out.println("geometryCopy对象中的rectangle矩形的长和宽");
            System.out.println(geometryCopy.rectangle.m + "," + geometryCopy.rectangle.n);
            System.out.println("geometry对象修改其中的rectangle矩形的长和宽");
            geometry.rectangle.m = 567.98;
            geometry.rectangle.n = 156.67;
            System.out.println("geometry对象中的rectangle矩形的长和宽:");
            System.out.println(geometry.rectangle.m + "," + geometry.rectangle.n);
            System.out.println("geometryCopy 对象中的 rectangle矩形的长和宽: ");
            System.out.println(geometryCopy.rectangle.m + "," + geometryCopy.rectangle.n);
        } catch (CloneNotSupportedException exp) {
        }
    }
}
